package springboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springboot.bean.Taskzc;
import springboot.bean.TaskzcExample;
import springboot.dao.TaskzcDAO;
import springboot.netty.SimpleServerHandler;
import springboot.tool.KernelTools;
import springboot.tool.SendMSGToPLC;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
public class TaskController {

    @Resource
    private TaskzcDAO taskzcDAO;

    private KernelTools km=new KernelTools();
    @Resource
    private SendMSGToPLC sendMSGToPLC;

    private Taskzc taskzcResend=null;
    @GetMapping("/resend")
    public void resendTask(int id,String resend){//是整条任务重发/取货重发/卸货重发
        log.info("id的值为{},发送的类型为{}",id,resend);
        taskzcResend=taskzcDAO.selectByPrimaryKey(id);
        if (resend.equals("whole")){
            km.CreateOrderOneStationResend(taskzcResend);
        }else if (resend.equals("unload")){
            km.CreateOrderSquenceEndResend(taskzcResend);
        }else if (resend.equals("load")){
            km.CreateOrderSquenceStartResend(taskzcResend);
        }
    }

    private List<Taskzc> listShow=null;
    private TaskzcExample taskzcExampleShow=null;
    private TaskzcExample.Criteria criteriaShow=null;
    private List<Integer> values=new ArrayList<>();
    @GetMapping("/showTaskInfo")
    public PageInfo showInfo(@RequestParam(defaultValue = "1",value = "page") int page, @RequestParam(defaultValue = "5") int pageSize){
        taskzcExampleShow=new TaskzcExample();
        criteriaShow=taskzcExampleShow.createCriteria();
        values.addAll(Arrays.asList(1,2,3,4,5,6,7,80,98,99));
        criteriaShow.andStatusIn(values);
        PageHelper.startPage(page,pageSize);
        listShow=taskzcDAO.selectByExample(taskzcExampleShow);
        PageInfo pageInfo=new PageInfo(listShow);
        return pageInfo;
    }

    private Taskzc taskzcCancel=null;
    @GetMapping("/cancel")
    public void cancel(int id){
        taskzcCancel=new Taskzc();
        taskzcCancel.setStatus(100);
        taskzcCancel.setId(id);
        taskzcDAO.updateByPrimaryKeySelective(taskzcCancel);
    }

    private TaskzcExample taskzcExampleCount=null;
    private TaskzcExample.Criteria criteriaCount=null;
    private Date date=null;
    private Date timestampStart=null;
    private Date timestampEnd=null;
    private SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private int countDay;
    private int countDayStart;
    private int countDayEnd;
    private List<Integer> count=null;
    String dateString=null;
    @GetMapping("/getCount")
    public List<Integer> getCount() throws ParseException {
        count=new ArrayList<>();
        taskzcExampleCount=new TaskzcExample();
        criteriaCount=taskzcExampleCount.createCriteria();
        //当天开始的时间
        date = new Date();
        dateString=sdf1.format(date);
        timestampStart = sdf2.parse(dateString+" 00:00:00");
        //当天结束的时间
        timestampEnd = new Date();
        criteriaCount.andStarttimeBetween(timestampStart,timestampEnd);
        countDay=(int)taskzcDAO.countByExample(taskzcExampleCount);
        count.add(countDay);
        //白班开始的时间
        timestampStart = sdf2.parse(dateString+" 00:00:00");
        //白班结束的时间
        timestampEnd = sdf2.parse(dateString+" 12:00:00");
        taskzcExampleCount=new TaskzcExample();
        criteriaCount=taskzcExampleCount.createCriteria();
        criteriaCount.andStarttimeBetween(timestampStart,timestampEnd);
        countDayStart=(int)taskzcDAO.countByExample(taskzcExampleCount);
        count.add(countDayStart);
        //夜班开始的时间
        timestampStart = sdf2.parse(dateString+" 12:00:00");
        //夜班结束的时间
        timestampEnd = sdf2.parse(dateString+" 23:59:59");
        taskzcExampleCount=new TaskzcExample();
        criteriaCount=taskzcExampleCount.createCriteria();
        criteriaCount.andStarttimeBetween(timestampStart,timestampEnd);
        countDayEnd=(int)taskzcDAO.countByExample(taskzcExampleCount);
        count.add(countDayEnd);
        //全天的
        sendMSGToPLC.sendMSGtoPLC(countDay,4);
        //白班的
        sendMSGToPLC.sendMSGtoPLC(countDayStart,8);
        //夜班的
        sendMSGToPLC.sendMSGtoPLC(countDayEnd,12);
        return count;
    }
}
